package com.dj.ms.utils;

import org.apache.commons.lang.StringUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * <br>
 * <p></p>
 *
 * <br>
 *
 * @author ZJG
 * @version 1.0
 * @date 2021/8/4 下午1:48
 */
public class ReflectUtil {

    public static boolean isCglibProxy(Class<?> beanClass){
        return hasField("CGLIB$BOUND",beanClass);
    }

    public static boolean hasField(String name, Class<?> clazz){
        if(StringUtils.isBlank(name) || clazz == null){
            return false;
        }
        try {
            clazz.getDeclaredField(name);
        }catch(NoSuchFieldException e){
            try {
                clazz.getField(name);
            }catch(NoSuchFieldException e1){
                return false;
            }
        }
        return true;
    }

    public static Field getField(String name, Class<?> clazz){
        if(StringUtils.isBlank(name) || clazz == null){
            return null;
        }
        try {
            return clazz.getDeclaredField(name);
        }catch(NoSuchFieldException e){
            try {
                return clazz.getField(name);
            }catch(NoSuchFieldException e1){
                return null;
            }
        }
    }

    public static Method getMethod(String name, Class<?> clazz,Class<?>...paramter){
        if(StringUtils.isBlank(name) || clazz == null){
            return null;
        }
        try {
            return clazz.getDeclaredMethod(name,paramter);
        }catch(NoSuchMethodException e){
            try {
                return clazz.getMethod(name,paramter);
            }catch(NoSuchMethodException e1){
                return null;
            }
        }
    }
}
